package com.neuedu.shixun.hospital2.service.Impl;
import com.neuedu.shixun.hospital2.entity.*;
import com.neuedu.shixun.hospital2.repository.*;
import com.neuedu.shixun.hospital2.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private Order2Repository order2Repository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private BingRepository bingRepository;
    @Autowired
    private DoctorRepository doctorRepository;
    @Override
    public List<Order> getOrder() {
        return orderRepository.findAll();
    }
    @Override
    public List<Order2> getOrder2() {
        return order2Repository.findAll();
    }
    @Override
    public List<Order2> getOrder2(List<Order> list) {
       List<Order2> list1=new ArrayList<>();
       for (Order order:list){
           Order2 order2=new Order2();
           order2.setOOid(order.getOOid());
           Optional<User> optionalUser=userRepository.findById(order.getUUid());
           User user=new User();
           if (optionalUser.isPresent()){
              user=optionalUser.get();
           }
           order2.setUUname(user.getUname());
           Optional<JiBing> optionalJiBing=bingRepository.findById(order.getBBid());
           JiBing jiBing=new JiBing();
           if (optionalJiBing.isPresent()){
               jiBing=optionalJiBing.get();
           }
           order2.setBBname(jiBing.getJbname());
           Optional<Doctor> optionalDoctor=doctorRepository.findById(order.getDDid());
           Doctor doctor=new Doctor();
           if (optionalDoctor.isPresent()){
               doctor=optionalDoctor.get();
           }
           order2.setDDname(doctor.getDDname());
           order2.setDJobnum(doctor.getDJobnum());
           log.info("status"+order.getOStatus());
         order2.setOStatus(order.getOStatus());

           order2.setOTimes(order.getOTimes());

           if (order.getOStatus()){
               long otime=order.getOTimes().getTime();
               long now=new Date().getTime();
               if (now>otime)
               order2.setOStatus1("过期");
               else
               order2.setOStatus1("等待");
           }
           else
               order2.setOStatus1("完成");
           list1.add(order2);
       }
       return list1;
    }
    @Override
    public List<Order> getOrderByDid(int did) {
        return orderRepository.findByDDid(did);
    }

    @Override
    public List<Order> getOrderByJobnum(String jobnum) {
        return orderRepository.findAll();
    }

    @Override
    public Order2 updateOrder2(Order2 order2) {
        return order2Repository.save(order2);
    }

    @Override
    public Order updateOrder(Order order) {
        return orderRepository.save(order);
    }

    @Override
    public Order getOrderOne(Integer oid,Boolean status) {
        Order order=orderRepository.getOne(oid);
        order.setOStatus(status);
        return order;
    }
}
